import { _decorator, animation, Animation, AnimationClip, Component, instantiate, Node, Prefab, resources, Sprite, SpriteFrame, Vec2 } from 'cc';
import { Direction } from '../Define';
import { TANK_CONFIG_MAP, TANK_RES_PATH, Vec2Dir } from '../Const';
import { Bullet } from '../Bullet';
import { myGame } from '../MyGame';
import { clipManager } from '../manager/ClipManager';
import { AudioManager } from '../manager/AudioManager';
const { ccclass, property } = _decorator;

const TempVec2 = new Vec2()

@ccclass('Tank')
export class Tank extends Component {
    @property
    public speed: number = 50

    @property(Prefab)
    public bulletPrefab: Prefab = null

    public curSpeed: number = 0

    public direction: Direction = Direction.Left

    public forward: Vec2 = Vec2Dir.Left

    public tank: Node = null

    public configId: number = 0

    onLoad() {
        this.tank = this.node.getChildByName('Sprite')
    }

    initWithConfig(configId: number) {
        this.configId = configId
        const cfg = TANK_CONFIG_MAP[configId]
        if (!cfg) {
            console.error('坦克配置不存在:', configId)
            return
        }
        this.speed = cfg.speed
        this.loadAnimationClip()
    }

    setDirection(dir: Direction) {
        this.direction = dir
        this.curSpeed = this.speed
        switch (dir) {
            case Direction.Left:
                this.tank.angle = 0
                this.forward = Vec2Dir.Left
                break
            case Direction.Right:
                this.tank.angle = -180
                this.forward = Vec2Dir.Right
                break
            case Direction.Up:
                this.tank.angle = -90
                this.forward = Vec2Dir.Up
                break
            case Direction.Down:
                this.tank.angle = 90
                this.forward = Vec2Dir.Down
                break
            default:
                break
        }

        this.node.getComponent(Animation).play()
    }

    getDesireDestination(dt: number) {
        const dis = dt * this.curSpeed
        const pos = this.node.position
        return TempVec2.set(this.forward.x * dis + pos.x, this.forward.y * dis + pos.y)
    }

    stopMove() {
        this.curSpeed = 0
        this.node.getComponent(Animation).stop()
    }

    shoot() {
        myGame.bulletManager.createBullet(this)
    }

    frameMove(dt: number) {

    }

    getCamp() {
        return null
    }

    loadAnimationClip() {
        const cfg = TANK_CONFIG_MAP[this.configId]
        if (!cfg) {
            console.error('坦克配置不存在:', this.configId)
            return
        }

        const clip = clipManager.createTankAnimation(cfg.res, cfg.clipName)
        if (!clip) {
            console.error('动画剪辑创建失败:', this.configId)
            return
        }

        const animCmp = this.node.getComponent(Animation)
        if (animCmp.clips.indexOf(clip) == -1) {
            animCmp.addClip(clip)
            animCmp.defaultClip = clip
        }

        this.tank.getComponent(Sprite).spriteFrame = resources.get(cfg.res + '_1/spriteFrame')
    }

    die() {

    }

    isInvincible() {
        return false
    }
}


